package com.shanjupay.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.common.domain.CommonErrorCode;
import com.shanjupay.merchant.api.AppService;
import com.shanjupay.merchant.api.dto.AppDTO;
import com.shanjupay.merchant.convert.AppCovert;
import com.shanjupay.merchant.entity.App;
import com.shanjupay.merchant.entity.Merchant;
import com.shanjupay.merchant.mapper.AppMapper;
import com.shanjupay.merchant.mapper.MerchantMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

/**
 * @program: shanjupay
 * @description:
 * @author: Mr.Hkc
 * @create: 2020-08-19 09:48
 **/

@Service
public class AppServiceImpl implements AppService {

    @Autowired
    private AppMapper appMapper;

    @Autowired
    private MerchantMapper merchantMapper;

    /**
     * 创建应用
     *
     * @param merchantId 商户ID
     * @param appDTO     应用信息
     * @return
     * @throws BusinessException
     */
    public AppDTO createApp(Long merchantId, AppDTO appDTO) throws BusinessException {
        //校验合法性
        if (merchantId == null || appDTO == null || StringUtils.isBlank(appDTO.getAppName())) {
            throw new BusinessException(CommonErrorCode.E_300009);
        }
        //1.校验商户资质是否通过审核
        Merchant merchant = merchantMapper.selectById(merchantId);
        if (merchant == null) {
            throw new BusinessException(CommonErrorCode.E_200002);
        }
        String auditStatus = merchant.getAuditStatus();
        if (!"2".equals(auditStatus)) {
            throw new BusinessException(CommonErrorCode.E_200003);
        }
        //应用名称唯一性
        String appName = appDTO.getAppName();
        Boolean existAppName = isExistAppName(appName);
        if (existAppName) {
            throw new BusinessException(CommonErrorCode.E_200004);
        }
        //2.生成应用id
        String appId = UUID.randomUUID().toString();
        App entity = AppCovert.INSTANCE.dto2entity(appDTO);
        entity.setAppId(appId);
        entity.setMerchantId(merchantId);
        //调用mapper对应用表插入数据
        appMapper.insert(entity);
        return AppCovert.INSTANCE.entity2dto(entity);
    }

    /**
     * 查询商家下的应用列表
     *
     * @param merchantId
     * @return
     * @throws BusinessException
     */
    public List<AppDTO> queryAppByMerchant(Long merchantId) throws BusinessException {
        List<App> appList = appMapper.selectList(new LambdaQueryWrapper<App>().eq(App::getMerchantId, merchantId));
        return AppCovert.INSTANCE.listentity2dto(appList);
    }

    /**
     * 根据业务id查询商家应用
     *
     * @param id
     * @return
     * @throws BusinessException
     */
    public AppDTO getAppById(String id) throws BusinessException {
        App app = appMapper.selectOne(new LambdaQueryWrapper<App>().eq(App::getAppId, id));
        return AppCovert.INSTANCE.entity2dto(app);
    }

    /**
     * 查询某个应用是否属于某个商户
     * @param appId
     * @param merchantId
     * @return
     */
    public Boolean queryAppInMerchant(String appId, Long merchantId) {
        int count = appMapper.selectCount(new LambdaQueryWrapper<App>()
                .eq(App::getAppId, appId)
                .eq(App::getMerchantId, merchantId));
        return count > 0;
    }


    //判断应用名称是否存在
    private Boolean isExistAppName(String appName) {
        Integer count = appMapper.selectCount(new LambdaQueryWrapper<App>().eq(App::getAppName, appName));
        return count > 0;
    }

}

